package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.cart.client.CartFeignClient;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 购物车管理
 *
 *      加入购物车
 */
@Controller
public class CartController {

    @Autowired
    private CartFeignClient cartFeignClient;



    //加入购物车
    @GetMapping("/addCart.html")
    public String addCart(Long skuId, Integer skuNum, RedirectAttributes redirectAttributes){

        //返回值应该是商品对象
        CartInfo cartInfo = cartFeignClient.addCart(skuId,skuNum);
        //cartInfo.setSkuNum(skuNum);
        //model.addAttribute("cartInfo",cartInfo);
        redirectAttributes.addAttribute("pid",skuId);
        redirectAttributes.addAttribute("sn",skuNum);
        return "redirect:http://cart.gmall.com/toCart.html";
    }

    //重定向到 购物车添加成功页面
    @GetMapping("/toCart.html")
    public String toCart(Long pid, Integer sn, Model model){
        //返回值应该是商品对象
        CartInfo cartInfo = cartFeignClient.toCart(pid);
        if (cartInfo != null){
            cartInfo.setSkuNum(sn);
            model.addAttribute("cartInfo",cartInfo);
            return "cart/addCart";
        }else {
            //未登录变成以登录后购物车没有数据，防止空指针异常重定向到首页
            return "redirect:http://www.gmall.com";
        }
    }

    //去购物车结算
    @GetMapping("/cart.html")
    public String cart(){

        return "cart/index.html";
    }


}
